package nlp

import (
	"errors"
	// "gcp-api-proxy/internal/conf"
	appRender "gcp-api-proxy/internal/rest/render"
	"gcp-api-proxy/pkg/gcp/apis"
	"gcp-api-proxy/pkg/util"
	"github.com/go-chi/render"
	"net/http"
)

type EntityExtractInput struct {
	Content string `json:"content"`
}

func (s *EntityExtractInput) Bind(r *http.Request) error {
	if util.IsEmpty(s.Content) {
		return errors.New("Conent字段为空")
	}
	return nil
}

type EntityExtractOutput struct {
	Data *struct {
		Answer string `json:"answer"`
	} `json:"data_list"`
	ErrCode int `json:"err_code"`
}

func (s *NLPRouter) entity_extract(w http.ResponseWriter, r *http.Request) {

	data := &EntityExtractInput{}
	if err := render.Bind(r, data); err != nil {
		render.Render(w, r, appRender.ErrBadRequest(err))
		return
	}
	ctx := r.Context()

	// client, err := apis.New(ctx, apis.WithEndPointProxy(s.GetProxy()), apis.WithAPIKeyProxy(s.GetProxy()))
	client, err := apis.New(ctx)
	if err != nil {
		render.Render(w, r, appRender.ErrRender(err))
		return
	}
	defer client.Close()

	entities, err := client.ExtractEntities(ctx, data.Content)
	if err != nil {
		render.Render(w, r, appRender.ErrRender(err))
		return
	}

	if err := render.Render(w, r, appRender.ListToRender(entities)); err != nil {
		render.Render(w, r, appRender.ErrRender(err))
		return
	}
}
